#include<bits/stdc++.h>
#define N 220000
#define eps 1e-7
#define inf 1e9+7
#define db double
#define ll long long
#define ldb long double
using namespace std;
inline int read()
{
	char ch=0;
	int x=0,flag=1;
	while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*flag;
}
struct edge{int to,nxt,w,o;}e[N*2];
int num,head[N];
inline void add(int x,int y,int z)
{
	e[++num]={y,head[x],z,z};head[x]=num;
	e[++num]={x,head[y],z,z};head[y]=num;
}
struct EDGE{int to,nxt,w;}E[N*2];
int NUM,HEAD[N];
inline void ADD(int X,int Y,int Z)
{
	E[++NUM]={Y,HEAD[X],Z};HEAD[X]=NUM;
	E[++NUM]={X,HEAD[Y],Z};HEAD[Y]=NUM;
}
queue<int>q;
int n,m,s,t,dep[N],cur[N];
bool bfs()
{
	for(int i=0;i<=n;i++)dep[i]=0,cur[i]=head[i];
	q.push(s);dep[s]=1;
	while(!q.empty())
	{
		int x=q.front();q.pop();
		for(int i=head[x];i!=-1;i=e[i].nxt)
		{
			int to=e[i].to;
			if(!dep[to]&&e[i].w)dep[to]=dep[x]+1,q.push(to);
		}
	}
	return dep[t];
}
int dfs(int x,int flow)
{
	if(x==t)return flow;
	for(int i=cur[x];i!=-1;i=e[i].nxt)
	{
		int to=e[i].to;
		if(e[i].w&&dep[to]==dep[x]+1)
		{
			int w=dfs(to,min(flow,e[i].w));
			if(w){e[i].w-=w;e[i^1].w+=w;return w;}
		}
	}
	return 0;
}
int dinic()
{
	int ans=0;
	while(bfs())ans+=dfs(s,inf);
	return ans;
}
bool flag[N];
void work(int x)
{
	flag[x]=true;
	for(int i=head[x];i!=-1;i=e[i].nxt)
	{
		int to=e[i].to;
		if(flag[to]||!e[i].w)continue;
		work(to);
	}
}
void solve(vector<int>v)
{
	if(v.size()<=1)return;
	vector<int>vl,vr;
	int len=v.size();s=v[0];t=v[len-1];
	for(int i=0;i<=n;i++)flag[i]=false;
	for(int i=0;i<=num;i++)e[i].w=e[i].o;
	int ans=dinic();ADD(s,t,ans);work(s);
	for(int i=0;i<len;i++)
	{
		int x=v[i];
		if(flag[x])vl.push_back(x);
		else vr.push_back(x);
	}
	solve(vl);solve(vr);
}
int d[N],w[N][22],nxt[N][22];
void get(int x,int fa,int t)
{
	d[x]=t;
	for(int i=HEAD[x];i!=-1;i=E[i].nxt)
	{
		int to=E[i].to;
		if(to==fa)continue;
		get(to,x,t+1);w[to][0]=E[i].w;nxt[to][0]=x;
	}
}
int dis(int x,int y)
{
	int ans=inf;
	if(d[x]<d[y])swap(x,y);
	for(int i=20;i>=0;i--)
	if(d[nxt[x][i]]>=d[y])
	ans=min(ans,w[x][i]),x=nxt[x][i];
	
	if(x==y)return ans;
	for(int i=20;i>=0;i--)if(nxt[x][i]!=nxt[y][i])
	ans=min(ans,min(w[x][i],w[y][i])),x=nxt[x][i],y=nxt[y][i];
	
	return min(ans,min(w[x][0],w[y][0]));
}
int main()
{
	vector<int>v;n=read()+1;m=read();
	for(int i=1;i<=n;i++)v.push_back(i);
	num=-1;memset(head,-1,sizeof(head));
	NUM=-1;memset(HEAD,-1,sizeof(HEAD));
	for(int i=1;i<=m;i++){int x=read()+1,y=read()+1,z=read();add(x,y,z);}
	solve(v);get(1,1,1);w[1][0]=inf;nxt[1][0]=1;
	for(int k=1;k<=20;k++)for(int i=1;i<=n;i++)
	{
		int to=nxt[i][k-1];
		nxt[i][k]=nxt[to][k-1];
		w[i][k]=min(w[i][k-1],w[to][k-1]);
	}
	int q=read();
	for(int i=1;i<=q;i++){int x=read()+1,y=read()+1;printf("%d\n",dis(x,y));}
	return 0;
}
